<?php

namespace app\common\model;

/**=========================================
 * Copyright © 2023-2024, WICMS.CN. All Rights Reserved
 * Project Name: WiCMS
 * Description: 路由导航菜单模型
 * Author: jason
 * Email: 52527998@QQ.COM
 * Date: 2024/9/18
 * Time: 上午10:48
 * File: app\common\Model\RouteMenuModel.php
 * =========================================
 */

use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\Model;

class RouteMenuModel extends Model
{

    protected $pk                 = 'id';
    protected $name               = 'RouteMenu';
    protected $autoWriteTimestamp = false;

    protected $json      = ['options'];
    protected $jsonAssoc = true;

    /**
     * 获取单条数据
     *
     * @param array $condition 查询条件
     *
     * @return array
     */
    public static function getInfo(array $condition = []): array
    {
        $result = (new self)->where(convertModelCondition($condition))->findOrEmpty();
        if ($result->isEmpty()) {
            return [];
        } else {
            return $result->toArray();
        }
    }

    /**
     * 获取路由导航菜单列表
     *
     * @param array  $condition 查询条件
     * @param string $order     排序方式
     * @param array  $field     查询字段
     *
     * @return array|RouteMenuModel[]
     */
    public static function getList(array $condition = [], string $order = 'rank desc', array $field = []): array
    {
        return (new self)->where(convertModelCondition($condition))->field($field)->order($order)->select()->toArray();
    }


    /**
     * 分页查询数据
     *
     * @param array  $condition
     * @param int    $limit
     * @param string $order
     * @param array  $field
     * @param array  $page_config
     *
     * @return array
     */
    public static function getPage(array $condition = [], int $limit = 20, string $order = 'rank desc', array $field = [], array $page_config = []): array
    {
        $page_config = array_merge($page_config, [
            'list_rows' => $limit,
        ]);
        try {
            $result = (new self)->where(convertModelCondition($condition))
                                ->field($field)
                                ->order($order)
                                ->paginate($page_config);
            return $result->toArray();
        } catch (DbException) {
            return [];
        }
    }

    /**
     * 保存数据
     *
     * @param array $data
     *
     * @return array
     */
    public static function saveData(array $data = []): array
    {
        // 根据data中是否存在id 来判断是添加还是修改
        $id = $data['id'] ?? 0;
        if ($id) {
            $result = self::update($data, ['id' => $id]);
        } else {
            $result = self::create($data);
        }
        return $result->toArray();
    }

    /**
     * 删除数据
     *
     * @param array $condition 删除条件
     * @param bool  $force     是否强制删除
     *
     * @return boolean
     */
    public static function deleteData(array $condition = [], bool $force = false): bool
    {
        return self::destroy(convertModelCondition($condition), $force);
    }

}